package com.muyi.service.impl;

import com.muyi.dao.UserDao;
import com.muyi.dao.impl.UserDaoImpl;
import com.muyi.pojo.entity.Account;
import com.muyi.pojo.entity.User;
import com.muyi.pojo.vo.Result;

import java.util.List;

public class UserServiceImpl implements com.muyi.service.UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public Result addUser(User user) throws Exception {
        //先判断用户是否存在
        boolean b1 = userDao.judgeUserExists(user.getNickname(), user.getGender(), user.getContact());
        if (!b1) {
            return new Result(-1, "用户已经存在,无法添加");
        }
        //添加用户信息
        int i = userDao.addUser(user);
        if (i > 0) {
            return new Result();
        }
        return new Result(-1, "添加用户失败");
    }

    @Override
    public Result deleteUserById(Integer user_id) throws Exception {
        int i = userDao.deleteUserById(user_id);
        if (i == 0) {
            return new Result(-1, "删除用户失败");
        }
        return new Result();
    }

    @Override
    public Result updateUser(User user) throws Exception {
        int i = userDao.updateUser(user);
        if (i == 0) {
            return new Result(-1, "修改用户信息失败");
        }
        return new Result();
    }

    @Override
    public Result getUserById(Integer user_id) throws Exception {
        User user = userDao.getUserById(user_id);
        if (null == user) {
            return new Result(-1, "查询用户信息失败");
        }
        Result result = new Result();
        result.setData(user);
        return result;
    }

    @Override
    public Result register(Account account) throws Exception {
        int register = userDao.register(account);
        if (register == 0) {
            return new Result(-1, "注册失败");
        }
        return new Result();
    }

    @Override
    public Result login(String username, String pwd) throws Exception {
        Account login = userDao.login(username, pwd);
        if (null == login) {
            //登录失败
            return new Result(-1, "登录失败");
        }
        Result result = new Result();
        result.setData(login);
        return result;
    }

    @Override
    public Result getUserBySearch(User user, Integer pageNo, Integer pageSize) throws Exception {
        //动态查询用户信息
        List<User> userList = userDao.getUserBySearch(user, pageNo, pageSize);
        //查询总数量
        long total = userDao.getTotal(user);
        //封装result
        Result result = new Result();
        result.setData(userList);
        result.setTotal(total);
        return result;
    }

    @Override
    public Result deleteuserByIds(Integer[] ids) throws Exception {
        int i = userDao.deleteUserByIds(ids);
        if (i == 0) {
            return new Result(-1, "删除失败");
        }
        return new Result();
    }
}
